package ru.vsu.cs.pm.beans;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.vsu.cs.pm.models.Account;
import ru.vsu.cs.pm.models.AccountType;
import ru.vsu.cs.pm.models.ItemType;
import ru.vsu.cs.pm.models.User;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Arrays;
import java.util.List;

/**
 * User: Oleg
 * Date: 23.12.2010
 * Time: 3:52:00
 */
@Component
public class AccountManagerImpl implements AccountManager {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional(readOnly = true)
    public List<Account> getAccountsForUser(User user) {
        return entityManager.createNamedQuery("account.get.by.user.id.and.account.type")
                .setParameter("userId", user.getId())
                .setParameter("accountTypes", Arrays.asList(AccountType.DEFAULT))
                .getResultList();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Account> getAccountsForUser(User user, int start, int count) {
        return entityManager.createNamedQuery("account.get.by.user.id")
                .setParameter("userId", user.getId())
                .setFirstResult(start)
                .setMaxResults(count)
                .getResultList();
    }

    @Override
    @Transactional(readOnly = true)
    public int getNumberOfUserAccounts(User user) {
        return ((Long) entityManager.createNamedQuery("account.get.number.by.user.id.and.account.type")
                .setParameter("userId", user.getId())
                .setParameter("accountTypes", Arrays.asList(AccountType.DEFAULT, AccountType.RESERVE))
                .getSingleResult()).intValue();
    }

    @Override
    @Transactional(readOnly = true)
    public Account getAccount(int id) {
        return entityManager.find(Account.class, id);
    }

    @Override
    @Transactional
    public Account saveOrUpdateAccount(Account account) {
        Account updatedAccount = null;
        if (account.getId() == null) {
            entityManager.persist(account);
        } else {
            Account accountToUpdate = getAccount(account.getId());
            if (accountToUpdate != null) {
                updatedAccount = entityManager.merge(account);
            } else {
                //TODO: log warning
            }
        }
        return updatedAccount == null ? account : updatedAccount; 
    }

    @Override
    @Transactional
    public Account getReserveAccountForUser(User user) {
        if (user == null || user.getId() == null) {
            throw new IllegalArgumentException("User is null!");
        }
        return (Account) entityManager.createNamedQuery("account.get.by.user.id.and.account.type")
                .setParameter("userId", user.getId())
                .setParameter("accountTypes", Arrays.asList(AccountType.RESERVE))
                .getSingleResult();
    }
}
